package com.moetube.search.controller;


import com.alibaba.fastjson.JSON;
import com.moetube.entity.Page;
import com.moetube.search.pojo.VideoInfo;
import com.moetube.search.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Controller
@RequestMapping("/search")
public class SearchController {

    @Autowired
    private SearchService searchService;

    @GetMapping("/toList")
    public String toList() {
        return "search";
    }

    @GetMapping("/list")
    public String list(@RequestParam Map<String, String> searchMap, Model model) {
        //处理查询条件
        //this.handleSearchMap(searchMap);
        Map resultMap = null;
        try {
            resultMap = searchService.search(searchMap);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("查询失败，有可能是请求参数里的页面参数有误");
        }
        model.addAttribute("searchMap", searchMap);
        model.addAttribute("resultMap", resultMap);

        if (resultMap != null && resultMap.size() > 0) {
            //设置分页参数
            //总页数
            //当前页
            //每页显示多少条
            Page<VideoInfo> page = new Page<VideoInfo>(
                    Long.parseLong(String.valueOf(resultMap.get("total"))),
                    Integer.parseInt(String.valueOf(resultMap.get("pageNum"))),
                    Page.pageSize
            );
            model.addAttribute("page", page);
        }

        //拼接访问参数
        StringBuilder url = new StringBuilder("/search/list");
        model.addAttribute("map", JSON.toJSONString(searchMap));
        if (searchMap != null && searchMap.size() > 0) {
            //有查询条件
            url.append("?");
            for (String paramKey : searchMap.keySet()) {
                if (!"sortRule".equals(paramKey) && !"sortField".equals(paramKey) && !"pageNum".equals(paramKey)&& !"time".equals(paramKey)&& !"tids_1".equals(paramKey)) {
                    url.append(paramKey).append("=").append(searchMap.get(paramKey)).append("&");
                }
            }
            String urlString = url.toString();
            //去除路径上最后一个&
            urlString = urlString.substring(0, urlString.length() - 1);
            model.addAttribute("url", urlString);
        } else {
            model.addAttribute("url", url);
        }
        return "search";
    }

    /**
     * 查询页面
     *
     * @param searchMap 查询条件
     * @return
     */
    @GetMapping("/toSearch")
    @ResponseBody
    public Map search(@RequestParam Map<String, String> searchMap) {
        //处理查询条件
        //this.handleSearchMap(searchMap);
        //根据条件查询返回结果
        Map resultMap = null;
        try {
            resultMap = searchService.search(searchMap);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("查询失败，有可能是请求参数里的页面参数有误");
        }
        return resultMap;
    }

    /*public void handleSearchMap(Map<String,String> searchMap){
        Set<Map.Entry<String, String>> entries = searchMap.entrySet();
        for (Map.Entry<String, String> entry : entries) {
            if(entry.getKey().startsWith("spec_")){
                searchMap.put(entry.getKey(),entry.getValue().replace("+","%2B"));
            }
        }
    }*/

}
